package com.thon.service.project.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.thon.dao.project.ProjectDao;
import com.thon.dao.user.UserDao;
import com.thon.entity.project.ProjectContributionView;
import com.thon.entity.project.ProjectStatistic;
import com.thon.entity.user.User;
import com.thon.service.project.ProjectStatisticService;

@Repository
@Transactional
public class ProjectStatisticServiceImpl implements ProjectStatisticService {

	@Autowired
	private ProjectDao projectDao;

	@Autowired
	private UserDao userDao;

	public ProjectStatisticServiceImpl() {
	}

	@Override
	public List<ProjectStatistic> listStatisticList(Integer projectId, String by) {
		List<ProjectStatistic> pss = new ArrayList<ProjectStatistic>();
		List<Object> objects = projectDao.listProjectStatistiBy(projectId, by);
		try {
			if(objects!=null){
				for (Object object : objects) {
	
					Object[] o = (Object[]) object;
	
					DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
					Date date = df.parse(o[0].toString());
					Long count = Long.valueOf(o[1].toString());
	
					ProjectStatistic ps = new ProjectStatistic();
					ps.setDate(date);
					ps.setCount(count);
	
					pss.add(ps);
				}
			}
		} catch (NumberFormatException e) {
		} catch (ParseException e) {
		}
		return pss;
	}

	@Override
	public Long getTopestByDate(Integer projectId, Long date) {
		return projectDao.getTopestByDate(projectId, date);
	}

	@Override
	public List<ProjectContributionView> listTopContributionByPro(
			Integer projectId, String by) {
		List<Object> daoList;
		List<ProjectContributionView> pcvList = new ArrayList<ProjectContributionView>();
		if (by.equals("supports")) {
			daoList = projectDao.listTopSupports(projectId, 10);
			if (daoList != null) {
				try {
					for (Object object : daoList) {
						Object[] o = (Object[]) object;
						ProjectContributionView pcv = new ProjectContributionView();

						Integer pid = (Integer) o[0];
						Integer userId = (Integer) o[1];
						Long supportsums=0l;
						if(o[2]!=null){
							supportsums = Long.valueOf(o[2].toString());
						}
						Long supportDate=null;
						if(o[3]!=null){
							supportDate=(((Date)o[3]).getTime());
						}

						Object os = projectDao.getShare(projectId, userId);
						Object[] s = (Object[]) os;
						Integer shareId = null;
						if (s[0] != null) {
							shareId = (Integer) o[0];
						}
						Long clicks = 0l;
						if (s[1] != null) {
							clicks = Long.valueOf(s[1].toString());
						}
						Long shareDate = null;
						if (s[2] != null) {
							shareDate = ((Date) s[2]).getTime();
						}
						User user = userDao.get(userId);
						String userName = user.getName();

						Integer funds = user.getFund();

						pcv.setProjectId(pid);
						pcv.setUserId(userId);
						pcv.setShareId(shareId);
						pcv.setUserName(userName);
						pcv.setSupportsums(supportsums);
						pcv.setSupportDate(supportDate);
						pcv.setShareDate(shareDate);
						pcv.setClicks(clicks);
						pcv.setFunds(funds);

						pcvList.add(pcv);
					}
				} catch (NumberFormatException e) {
					System.out.println(e);
				}
			}

		}
		if (by.equals("shares")) {
			daoList = projectDao.listTopShares(projectId, 10);
			if (daoList != null) {
				try {
					for (Object object : daoList) {
						Object[] o = (Object[]) object;
						ProjectContributionView pcv = new ProjectContributionView();

						Integer pid = (Integer) o[0];
						Integer userId = (Integer) o[1];
						String userName = (String) o[2];
						Integer shareId = null;
						if (o[3] != null) {
							shareId = (Integer) o[3];
						}
						Long shareDate = null;
						if (o[4] != null) {
							shareDate = ((Date) o[4]).getTime();
						}

						Long clicks = 0l;
						if (o[5] != null) {
							clicks = Long.valueOf(o[5].toString());
						}
						Integer funds = (Integer) o[6];
						Long supportsums = projectDao.getSupportSum(projectId,
								userId);

						pcv.setProjectId(pid);
						pcv.setUserId(userId);
						pcv.setShareId(shareId);
						pcv.setUserName(userName);
						pcv.setSupportsums(supportsums);
						pcv.setShareDate(shareDate);
						pcv.setClicks(clicks);
						pcv.setFunds(funds);

						pcvList.add(pcv);
					}
				} catch (NumberFormatException e) {
					System.out.println(e);
				}
			}
		}
		return pcvList;
	}
}
